package CelestialDataAccess;

import CelestialDataAccess.CryptService;
import CelestialDataAccess.IFactory;
import CelestialDataAccess.IKey;
import CelestialDataAccess.User;
import javax.sql.rowset.*; //provides cached row set interface
import java.util.Date;

public final class Employee extends User implements Comparable, IKey<Integer>{

    private int m_iEmployeeID;
    private Date m_dtEmployedOn;
    
    private String m_strContact;
    private Date m_dtDOB;
    private String m_strAddressLine1;
    private String m_strAddressLine2;
    private String m_strPostalCode;

    private boolean m_bIsActive;

    private static IFactory<Employee> m_factory = new IFactory<Employee>(){

        public Employee createT(CachedRowSet crs){

            Employee emp;

            try{
                if(crs.getRow()<1) return null;

                emp = new Employee();
                emp.m_strPassword = CryptService.decrypt(crs.getBytes("Password"));
                emp.m_iUserLevel =crs.getInt("UserLevel");
                emp.m_iEmployeeID = crs.getInt("EmployeeID");
                emp.m_dtEmployedOn = crs.getDate("EmployedOn");

                emp.m_strFirstName = crs.getString("FirstName");
                emp.m_strLastName = crs.getString("LastName");
                emp.m_strContact = crs.getString("Contact");
                emp.m_strEmail = crs.getString("Email");
                emp.m_dtDOB = crs.getDate("DOB");
                emp.m_strAddressLine1 = crs.getString("AddressLine1");
                emp.m_strAddressLine2 = crs.getString("AddressLine2");
                emp.m_strPostalCode = crs.getString("PostalCode");

                emp.m_bIsActive  =crs.getBoolean("IsActive");
            }catch(Exception ex){
                 return null;
            }
            return emp;
        }
    };
    /*************************************************************************/
    public int getEmployeeID(){ return m_iEmployeeID;}
    /*-----------------------------------------------------------------------*/
    public Date getEmploymentDate(){ return m_dtEmployedOn;}
    public void setEmploymentDate(Date emp_date){ m_dtEmployedOn = emp_date; }
    /*-----------------------------------------------------------------------*/
    public String getContact(){return m_strContact;}
    public void setContact(String contact){ m_strContact = contact; }
    /*-----------------------------------------------------------------------*/
    public int getAge(){return 0;}
    public Date getDOB(){return m_dtDOB;}
    public void setDOB(Date dob){ m_dtDOB = dob; }
    /*-----------------------------------------------------------------------*/

    public String getAddressLine1(){return m_strAddressLine1;}
    public void setAddressLine1(String addr1){ m_strAddressLine1 = addr1; }
    /*-----------------------------------------------------------------------*/
    public String getAddressLine2(){return m_strAddressLine2;}
    public void setAddressLine2(String addr2){ m_strAddressLine2 = addr2; }
    /*-----------------------------------------------------------------------*/
    public String getPostalCode(){return m_strPostalCode;}
    public void setPostalCode(String pcode){ m_strPostalCode = pcode; }
    /*-----------------------------------------------------------------------*/

    public boolean isActive(){return m_bIsActive;}
    public void setActive(Boolean active){ m_bIsActive = active; }

    /*-----------------------------------------------------------------------*/
    static IFactory<Employee> getFactory(){ return m_factory; }

    /*************************************************************************/
    /* Constructors */
    public Employee(){
        m_iEmployeeID = 0;
        m_dtEmployedOn = new Date();
    }
    /*-----------------------------------------------------------------------*/
    static Employee createEmployee(CachedRowSet crs){ 
        return m_factory.createT(crs);
    }
    /*************************************************************************/
    /* IKey */
    public Integer getKey(){ return m_iEmployeeID; }

    /*************************************************************************/
    /* Comparable */
    public int compareTo(Object emp){

        if(this.m_iEmployeeID==((Employee)emp).m_iEmployeeID)
            return 0;
        else if(this.m_iEmployeeID > ((Employee)emp).m_iEmployeeID)
            return 1;
        else
            return -1;
    }
    /*************************************************************************/

    public String toString(boolean details){

        if(details)
            return m_iEmployeeID + ";   " + m_strPassword + ";   " + m_strFirstName + ";   " +
                    m_strLastName + ";   " + m_dtEmployedOn + ";   " + m_dtDOB + ";   " +
                    m_strEmail + ";   " + m_strContact + ";   " +
                    m_strAddressLine1 + ";   " + m_strAddressLine2 + ";   " +
                    m_strPostalCode;
        else
            return toString();
    }
    /*-----------------------------------------------------------------------*/
    public @Override String toString(){ return getName(); }
}
